package code.ws_common_scene.mode.base
{
	import code.ws_common_ems.ModeRenderEms;
	import code.ws_common_scene.texturedata.ModeTextureDataPool;
	import code.ws_common_scene.texturedata.TexturePlayData;
	import code.ws_common_scene.texturedata.Textures;
	import code.ws_common_utils.UrlUtils;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.utils.getDefinitionByName;
	
	import je.engine.loaderware.realtimeLoa.seq.JEAutoLoaSwf;
	
	import starling.display.Image;
	import starling.textures.Texture;
	import starling.utils.Color;

	/**
	 * 动作刷新器 
	 * @author qiangchen
	 * 
	 */	
	public class ViewClip extends Image
	{
		/** 渲染类型 */
		private var _render:String = null;
		/** 皮肤样式 */
		private var _style:String = null;
		/** 动作 */
		private var _action:String = null;
		/** 方向 */
		private var _dir:int;
		/** 动作样式加载器 */
		private var _loaStyle:JEAutoLoaSwf = null;
		/** 动作播放数据 */
		private var _playData:TexturePlayData = null;
		/** 当前帧 */
		private var _crtFrame:int = 0;
		/** 播放速率控制器 */
		private var _fr:int;
		/** 在循环播放体中，单循环播放是否已完成开关 */
		private var _isFlag:Boolean = false;
		/** 是否正在加载swf资源 */
		private var _isLoading:Boolean = false;
		/** 在动作循环播放过程中时实时回调方法 */
		private var _fnUpdata:Function;
		/** 在动作单循环播放后的回调方法 */
		private var _fnCom:Function;
		
		
		
		/**
		 * 构造函数 
		 * 
		 */		
		public function ViewClip()
		{
			super( Texture.fromBitmapData( new BitmapData(1,1,false,Color.BLACK), false ));
		}
		
		
		/**
		 * 初始化 
		 * @param render
		 * @param style
		 * @param action
		 * @param dir
		 */		
		public function ini( render:String, style:String, action:String, dir:int ):void
		{
			_render = render;
			_style = style;
			_action = action;
			_dir = dir;
			
			_playData = ModeTextureDataPool.getPlayData( selfRender, style, action, dir );
			if( _playData == null )
			{
				throw new Error("[ViewClip]--->(Error): TexturePlayData is null. style: " + style + " action: " + action + " dir: " + dir);
				return;
			}
			
			_isLoading = false;
		}
		
		
		/**
		 * 设置方向 
		 * @param dir
		 * 
		 */		
		public function setDirection( dir:int ):void
		{
			_dir = dir;
			_playData = ModeTextureDataPool.getPlayData( selfRender, style, action, dir );
			if( _playData == null )
			{
				throw new Error("[ViewClip]--->(Error): TexturePlayData is null. style: " + style + " action: " + action + " dir: " + dir);
				return;
			}
		}
		
		
		
		/**
		 * 设置动作 
		 * @param action
		 * @param fnUpdata 在动作播放一循环过程中时，需实时刷新的方法（默认：null）
		 * @param fnCom 在动作播放一循环后的回调方法（默认：null）
		 */		
		public function setAction( action:String, fnUpdata:Function=null, fnCom:Function=null ):void
		{
			_action = action;
			_fnUpdata = fnUpdata;
			_fnCom = fnCom;
			_crtFrame = 0, _fr = 0;
			
			_playData = ModeTextureDataPool.getPlayData( selfRender, style, _action, dir );
			if( _playData == null )
			{
				throw new Error("[ViewClip]--->(Error): TexturePlayData is null. style: " + style + " action: " + action + " dir: " + dir);
				return;
			}
			
			if( _loaStyle )
			{
				_loaStyle.autoRelease();
				_loaStyle = null;
			}
			
			_isLoading = false;
			_isFlag = false;
		}
		
		
		
		/**
		 * 刷新 
		 * 
		 */
		public function refresh():void
		{
			_fr ++;
			if( _fr >= _playData.frameRate )
			{
				_fr = 0;
				
				if( !_playData.isLoop && _isFlag ) return;
				
				var t:Textures = _playData.textures[_crtFrame];
				if( !t )
				{
					throw new Error("[ViewClip]--->(Error): texture play data is null.");
					return;
				}
				
				try
				{
					var _class:Class = getDefinitionByName( t.key ) as Class;
					var bmp:BitmapData = new _class() as BitmapData;
					
					this.texture = Texture.fromBitmapData( bmp, false );
					this.readjustSize();
					
					this.scaleX = t.scal;
					this.x = t.offx;
					this.y = t.offy;
					
					bmp.dispose();
					bmp = null;
					_class = null;
					
					// 动作过程回调方法
					if( _fnUpdata != null )
					{
						_fnUpdata.call( null );
					}
				}
				catch( e:Error )
				{
//					trace( e );
					
					if( !_isLoading )
					{
						_loaStyle = new JEAutoLoaSwf( loaCom, null, "", false);
						_loaStyle.startStram( getModeUrl(  _render, _playData.style + "_" + _playData.action  ) );

						_isLoading = true;
					}
				}
				
				_crtFrame ++;
				if( _crtFrame > _playData.textures.length -1 )
				{
					_crtFrame = 0;
					
					_isFlag = ( !_playData.isLoop ) ? true : false;
					
					// 动作完成单循环后执行回调方法
					if( _fnCom != null )
					{
						_fnCom.call( null );
					}
				}
			}
		}
		
		
		/**
		 * 获取需加载的swf文件资源地址 
		 * @param render
		 * @param key
		 * @return 
		 * 
		 */		
		private function getModeUrl( render:String , key:String ):String
		{
			if( render == ModeRenderEms.HERO )
			{
				return UrlUtils.getHerolUrl( key );
			}
			else if( render == ModeRenderEms.MONSTER )
			{
				return UrlUtils.getMonsterUrl( key );
			}
			
			throw new Error( "[ViewClip]--->(Error): Can not find url. Because: render_" + render + " / key_" + key );
			return null;
		}
		
		
		
		/**
		 * 动作播放数据器加载完成 
		 * @param mc
		 * 
		 */		
		private function loaCom( mc:MovieClip ):void
		{
			mc = null;
			_loaStyle.autoRelease();
			_loaStyle = null;
			
			_isLoading = false;
		}
		
		
		/**
		 * 获取皮肤样式 
		 * @return 
		 * 
		 */		
		public function get style():String
		{
			return _style;
		}
		
		
		/**
		 * 获取渲染类型 
		 * @return 
		 * 
		 */		
		public function get selfRender():String
		{
			return _render;
		}
		
		
		/**
		 * 获取动作 
		 * @return 
		 * 
		 */		
		public function get action():String
		{
			return _action;
		}
		
		
		/**
		 * 获取方向 
		 * @return 
		 * 
		 */		
		public function get dir():int
		{
			return _dir;
		}
		
		
		/**
		 * 获取播放数据 
		 * @return 
		 * 
		 */		
		public function get playData():TexturePlayData
		{
			return _playData;
		}
		
		
		
		/**
		 * 释放 
		 * 
		 */		
		public function die():void
		{
			if( _loaStyle )
			{
				_loaStyle.autoRelease();
				_loaStyle = null;
			}
			_playData = null;
			_crtFrame = NaN;
			_fr =NaN;
			_style = null;
			_action = null;
			_render = null;
			_dir = NaN;
			_fnCom = null;
			_fnUpdata = null;
		}
	}
}